package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.query.EmployeeQuery;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.JsonResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    //角色属性

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") EmployeeQuery qo, Model model){
        model.addAttribute("result",this.employeeService.query(qo));
        model.addAttribute("depts",this.departmentService.list());
        return "employee/list";
}

    @RequestMapping("/input")
    public String input(Long id,Model model){
        model.addAttribute("depts",this.departmentService.list());
        model.addAttribute("roles",this.roleService.list());
        if(id!=null){
            model.addAttribute("entity",this.employeeService.get(id));
        }
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee entity, Long[] ids){
        this.employeeService.saveOrUpdate(entity,ids);
        return new JsonResult("成功",true);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        this.employeeService.delete(id);
        return new JsonResult("操作成功",true);
    }

    //返回true 验证通过，反之不通过
    //false代表用户已存在，反之代表用户不存在
    //bootStrap-valid插件要求，必须有valid这个布尔值来判断是否存在，
    //所以要返回一个MAP,MAP的KEY就叫valid
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String name){
        Map map = new HashMap();
        map.put("valid",this.employeeService.checkName(name));
        return map;
    }

    @RequestMapping("/abtchDelete")
    @ResponseBody
    public JsonResult abtchDelete(Long[] ids){
        System.out.println(ids);
        this.employeeService.abtchDelete(ids);
        return new JsonResult("操作成功",true);
    }

    //导出
    @RequestMapping("/exprotxls")
    public void exportxls(HttpServletResponse response)throws IOException{
        //设置文件头像信息
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");

        //创建一个excel文件
        Workbook wb = employeeService.exporxls();
        //把excel的数据输出给浏览器
        wb.write(response.getOutputStream());
    }

    //导入
    @RequestMapping("/importxls")
    @ResponseBody
    //上传要用到的参数是MultipartFile
    public JsonResult importxls(MultipartFile file) throws IOException{
        employeeService.importxls(file);
        return new JsonResult(true);
    }
}
